// index.ts

import { IAppOption } from "../../appoption";
import { routing } from "../../utils/routing";

// 获取应用实例
const app = getApp<IAppOption>();

Page({
  isPageShowing: false,
  data: {
    setting: {
      skew: 0,
      rotate: 0,
      showScale: true,
      subKey: "",
      layerStyle: -1,
      enableZoom: true,
      enableScroll: true,
      enableRotate: true,
      showCompass: false,
      enable3D: false,
      enableOverlooking: false,
      enableSatellite: false,
      enableTraffic: false,
    },
    location: {
      latitude: 23.0999994,
      longitude: 113.32452,
    },
    scale: 10,
    markers: [
      {
        iconPath: "/resources/car.png",
        id: 0,
        latitude: 23.0999994,
        longitude: 113.32452,
        width: 50,
        height: 50,
      },
      {
        iconPath: "/resources/car.png",
        id: 1,
        latitude: 23.0999994,
        longitude: 114.32452,
        width: 50,
        height: 50,
      },
    ],
  },

  onMyLocationTap() {
    console.log("定位");
    wx.getLocation({
      type: "gcj02",
      success: (res) => {
        console.log(res);
        this.setData({
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
          },
        });
      },
      fail(res) {
        console.log("获取经纬度失败", res);
        wx.showToast({
          icon: "none",
          title: "前往设置获取权限",
        });
      },
    });
  },
  onMyTripTap() {
    wx.navigateTo({ url: routing.mytrips() });
  },
  onShow() {
    this.isPageShowing = true;
  },
  onHide() {
    this.isPageShowing = false;
  },
  //车辆移动
  moveCars() {
    const map = wx.createMapContext("map");
    const dest = {
      latitude: 23.0999994,
      longitude: 113.32452,
    };
    const moveCar = () => {
      dest.latitude += 0.1;
      dest.longitude += 0.1;
      map.translateMarker({
        destination: {
          latitude: dest.latitude,
          longitude: dest.longitude,
        },
        markerId: 0,
        autoRotate: false,
        rotate: 0,
        duration: 5000,
        animationEnd: () => {
          if (this.isPageShowing) {
            moveCar();
          }
        },
      });
    };
    moveCar();
  },
  //扫码租车
  onScanClicked() {
    wx.scanCode({
      success: () => {
        const carID = "carId";
        // const redirectUrl = `/pages/lock/lock?car_id=${carID}`;
        // wx.navigateTo({
        //   url: `/pages/register/register?redirect=${encodeURIComponent(
        //     redirectUrl
        //   )}`,
        // });
        const redirectURL = routing.lock({
          car_id: carID,
        });
        wx.navigateTo({
          url: routing.register({
            redirectURL: redirectURL,
          }),
        });
      },
      fail: () => {},
    });
  },
});
